package com.hexacta.hrs.spring;

import java.util.Collection;

import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.user.Person;

import com.hexacta.hrs.repositories.RecordObjectRepository;

/**
 * Objeto encargado de devolver la persona logueada.
 * 
 * @see https://wiki.hexacta.com/mediawiki/index.php/hrs:KB-5
 * 
 * @author tpollak
 */
public final class PersonRetriever {
    private RecordObjectRepository<Person> personGenericRepository;

    /**
     * Devuelve la persona logueada.
     */
    public Person getLoggedInPerson() {
        Person person = null;
        Person threadPerson = ThreadProperties.getPerson();
        if (threadPerson != null && threadPerson.getId() != null) {
            person = personGenericRepository.forId(threadPerson.getId());
        }
        return person;
    }

    public void setPersonGenericRepository(final RecordObjectRepository<Person> personGenericRepository) {
        this.personGenericRepository = personGenericRepository;
    }

    public Person findById(final Long id) {
        return personGenericRepository.forId(id);
    }
    
	public Collection<Person> searchByCriteria(Person criteria) {
		return personGenericRepository.matching(criteria);
	}    
}
